var waveHelper = require('../../utils/wave.js'); 
var HW = require('../../utils/hardware.js'); 
Page({
    data:{
        waveW:0,
        waveH:0,
        borderW:0,
        borderH:0,
        wave:'',
        Loading:false,
        hidden:true,
        modelText:'',
        linked:false,
        linkstate:'连接心率带',
        linking:false,
        deviceId:'',
        timer:'',
        heartPer:0,
        heart:0,
        MaxHeart:getApp().globalData.UserData.max_heart,
        avgHeart:0,
        startTime:'',
        endTime:'',
        highHeart:0,
        avgPer:0,
        highPer:0,
        lowHeart:0,
        runTime:'00:00:00',
        level_1:0,
        level_2:0,
        level_3:0,
        level_4:0,
        level_5:0,
        heartNum:0,
        runSec:0,
        calorie:0,
    },
    onShow(){
      this.setData({
        MaxHeart: getApp().globalData.UserData.max_heart
      })
    },
    canvasErrorCallback: function (e) {
        console.error(e.detail.errMsg)
    },
    confirm(){
        this.setData({hidden:true})
    },
    onLoad(option){
      var self = this;
        this.setData({
            startTime:new Date().toLocaleTimeString()
        })
        var w = getApp().globalData.screenW;
        var h = getApp().globalData.screenH;
        //0.85
        //left 0.26 right0.74
        var canvasW = w*0.63
        this.setData({
            waveW:canvasW,
            waveH:canvasW,
            borderW:canvasW*0.37,
            borderH:canvasW,
            deviceId:option.deviceId
        })
        var wave = waveHelper.wave('wave',canvasW,canvasW);
        wave.start(canvasW);
        this.setData({wave:wave});
        this.connectblue();
        this.runTimer();
        // setInterval(function(){
        //     var step = Math.random();
        //     self.setHeart(step*100);
        // },5000)
    },
    docalorie(){
      let heartper = this.data.avgPer;
      let calorie = this.data.calorie;
      if (heartper<0.5){
        return calorie+1;
      } else if (heartper < 0.6) {
        return calorie + 2;
      } else if (heartper < 0.7) {
        return calorie + 3;
      } else if (heartper < 0.8) {
        return calorie + 4;
      } else if (heartper < 0.9) {
        return calorie + 5;
      } else if (heartper < 1) {
        return calorie + 6;
      }else
        return calorie + 7;
    },
    runTimer(){
        let self = this;
        let Timer = setInterval(function(){
            let totalSec = self.data.runSec;
            let totalMin = Math.floor(totalSec/60);
            let hour = Math.floor(totalMin/60);
            let sec = totalSec-totalMin*60;
            let min = totalMin-hour*60;
            sec = sec>9?sec:"0"+sec;
            min = min>9?min:"0"+min;
            if(totalSec%15==0){
              self.setData({ runTime: "0" + hour + ":" + min + ":" + sec, runSec: self.data.runSec + 1, calorie: self.docalorie()})
            }else{
              self.setData({ runTime: "0" + hour + ":" + min + ":" + sec, runSec: self.data.runSec + 1})
            }
        },1000);
    },
    setHeart(value){
        let val = parseInt(value);
        console.log(val);
        let per = Math.round(val/this.data.MaxHeart);
        let hH=this.data.highHeart;
        let lH=this.data.lowHeart;
        if(val>this.data.highHeart)hH = val;
        else if(val<=this.data.lowHeart||0==this.data.lowHeart)lH = val;
        let avg = Math.round((hH+val+lH)/3);
        let hP = Math.round(hH * 100/this.data.MaxHeart);
        let avgP = Math.round(avg*100/this.data.MaxHeart);
        this.data.calorie+0.2;
        this.setData({ highHeart: hH, highPer: hP, heartPer: per, avgHeart: avg >= 0 ? avg : 0, avgPer: avgP, highPer: hP, heartNum: this.data.heartNum + 1})
        this.data.wave.step(per);
    },
    connectblue(){
        let self = this;
        let name;
        HW.connectblue(self,self.setHeart);
        HW.stateListener(self);
    },
    stop(){
        let self = this;
        this.setData({
            endTime:new Date().toLocaleTimeString(),
            Loading:true
        })
        let level_1per = this.data.level_1/this.data.heartNum;
        let level_2per = this.data.level_2/this.data.heartNum;
        let level_3per = this.data.level_3/this.data.heartNum;
        let level_4per = this.data.level_4/this.data.heartNum;
        let level_5per = this.data.level_5/this.data.heartNum;
        getApp().request({
            url:"upload_heart_record",
            data:{
                avg_heart:self.data.avgHeart,
                cost_time:self.data.runTime,
                max_heart:self.data.highHeart,
                level_1:level_1per,
                level_2:level_2per,
                level_3:level_3per,
                level_4:level_4per,
                level_5:level_5per,
                user_id:getApp().globalData.UserData.id,
                mac_addr:self.data.deviceId,
                carlorie: self.data.calorie
            },
            success(res){
              console.log(res);
                if(res.data.flag){
                  self.setData({hidden:false,modelText:'提交成功'})
                    wx.navigateBack({delta: 1})
                }else{
                  self.setData({ Loading: false, hidden: false, modalText: res.data.msg})
                }
            },fail(res){
              self.setData({ hidden: false, modelText: '提交失败', Loading: false})
            },complete(){
              self.setData({Loading:false})
            }
        })
        HW.closeblue(this);
    },proprotion(num){
		if(num<=0.6){
			this.setData({level_1:this.data.level_1+1})
		}else if(num<=0.7){
			this.setData({level_2:this.data.level_2+1})
		}else if(num<=0.8){
			this.setData({level_3:this.data.level_3+1})
		}else if(num<=0.9){
			this.setData({level_4:this.data.level_4+1})
		}else if(num<=1){
			this.setData({level_5:this.data.level_5+1})
		}
	}
    
})